package cn.com.self.apis.portal.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.entity.SysUserEntity;
import cn.com.self.apis.portal.service.intf.ISysUserRoleService;
import cn.com.self.apis.portal.service.intf.ISysUserService;
import cn.com.self.apis.portal.util.AESUtil;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户信息类")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

     /**新增用户
      * @Description //TODO
      * @return
      **/
     @RequestMapping(value = "/create", method = RequestMethod.POST)
     @ResponseBody
     @ApiOperation(value = "新增用户")
     public ResponseEntity create(@RequestBody SysUserEntity entity, HttpServletRequest request){
         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

         Date d = new Date();
         Timestamp t = new Timestamp(d.getTime());
         entity.setRegisterTime(t);
//         String code = entity.getCommunityCode();
////         if(code !=null && code !=""){
////             entity.setStoreId(Long.parseLong(entity.getCommunityCode()));
////         }
         //entity.setPassword(AESUtil.encrypt(entity.getPassword(),"china888"));
         entity.setPassword(entity.getPassword());
         SysUserEntity sysUserEntity = this.sysUserService.create(entity);
         return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysUserEntity)));
    }

     /**分页获取用户列表数据
      * @Description //TODO
      * @return
      **/
     @RequestMapping(value = "/page", method = RequestMethod.GET)
     @ResponseBody
     @ApiOperation(value = "分页获取用户列表数据")
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page){
         //Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort");

        //Page<SysUserEntity> pageData = this.sysUserService.page(queryParams, page);

         Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "page", "size","access_token");
         Page<SysUserEntity> pageData = sysUserService.selectPage(queryMap, page);
         return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataPage(pageData)));

    }


    /**用户详情
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "用户详情")
    public ResponseEntity loadUser(@PathVariable("id") String id,HttpServletRequest request) {
        SysUserEntity sysUserEntity = sysUserService.selectOne1(id);
        sysUserEntity.setPassword(AESUtil.decrypt(sysUserEntity.getPassword(),"china888"));
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysUserEntity)));

    }

     /**验证用户是否已存在
      *
      * @Description //TODO
      * @return
      **/
    @RequestMapping(value = {"/checkuser"}, method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "验证用户是否已存在")
    public ResponseEntity checkUser(HttpServletRequest request) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");

        SysUserEntity sysUserEntity = this.sysUserService.checkUser(queryParams);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysUserEntity)));

    }



    /**用户更新
      * @Description //TODO
      * @return
      **/
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户更新")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody SysUserEntity entity, HttpServletRequest request) {
      /* if(entity.getOrganizationType().equals("201")){
           entity.setCommunityCode("");
           entity.setCommunityName("");
       }
       if(entity.getOrganizationType().equals("202")){
           entity.setGovernmentCode("");
           entity.setGovernmentName("");
           entity.setAreaCode("");
           entity.setAreaName("");
       }*/
        //entity.setPassword(AESUtil.encrypt(entity.getPassword(),"china888"));
        entity.setPassword(entity.getPassword());
        SysUserEntity sysUserEntity = this.sysUserService.load(id);
        CopyUtils.copyProperties(entity, sysUserEntity);
        SysUserEntity info = this.sysUserService.update(sysUserEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));

    }
    /**用户更新密码
      * @Description //TODO
      * @return
      **/
    @RequestMapping(value = {"/updatePassword/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户更新密码")
    public ResponseEntity updatePassword(@PathVariable("id") String id, @RequestBody SysUserEntity entity, HttpServletRequest request) {
        //entity.setPassword(AESUtil.encrypt(entity.getPassword(),"china888"));
        entity.setPassword(entity.getPassword());
        SysUserEntity sysUserEntity = this.sysUserService.load(id);
        CopyUtils.copyProperties(entity, sysUserEntity);
        SysUserEntity info = this.sysUserService.update(sysUserEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));

    }


     /**删除用户
      * @Description //TODO
      * @return
      **/
     @ResponseBody
     @RequestMapping(value = "/deletes",method = RequestMethod.DELETE)
     @ApiOperation(value = "删除用户")
    public ResponseEntity deletes(@RequestBody List<Long> ids){
         this.sysUserService.deletes(ids);
         return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
     }




}
